X
Includes & partials in ejs allow you to include html templates into your app. In this example, we're going to include a header into our index ejs file. First, we're going to create a folder called partials, then we're going to make a header.ejs file & add our code.
Next, we're going to create the index.ejs file. We're going to put the include function where the header would be. The include function takes two arguments,
The first a string, it's a path to the partial file which can be either absolute or relative.
The second is an object, that is data that can be passed to the partial.
We can also pass data to the template using the second argument of the include function.
We can grab that data & use it in the partial. A partial shares the same data as the parent view.
You can use any script that you would be able to use in javascript in ejs. We're going to make another header called header2.ejs & use an if statement to pass the header that we want into our index page. The header will have most of the same content except 1 nav link will be missing. Make sure you begin & close every ejs script tag with <% %>. Define a variable called flag & set it to true. Then, we're going to make the if statement, if the flag is true we're going to show header2.ejs, else we're going to show header.ejs.
With all the new tech coming out, ejs is still simple & powerful to use.